#include "pidcontroller.h"
#include "ui_pidcontroller.h"
#include"monitordevice.h"//监测类
Q_GLOBAL_STATIC(PidController,theInstance);
PidController::PidController(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::PidController)
{
    ui->setupUi(this);
//温度
    connect(MonitorDevice::instance(),&MonitorDevice::sendAutoControlAirTemperature,this,&PidController::AutomaticTemperatureControl);
//光照强度
    connect(MonitorDevice::instance(),&MonitorDevice::sendAutoControlLightIntensity,this,&PidController::AutomaticLightIntensityControl);
//空气湿度
    connect(MonitorDevice::instance(),&MonitorDevice::sendAutoControlAirHumidity,this,&PidController::AutomaticAirHumidityControl);
//CO2浓度
    connect(MonitorDevice::instance(),&MonitorDevice::sendAutoControlCO2Concentration,this,&PidController::AutomaticCO2ConcentrationControl);
//土壤湿度
    connect(MonitorDevice::instance(),&MonitorDevice::sendAutoControlSoilMoisture,this,&PidController::AutomaticSoilMoistureControl);


}

PidController::~PidController()
{
    delete ui;
}

PidController *PidController::instance()
{
return theInstance;
}

void PidController::AutomaticTemperatureControl(int status)//true为过高，false为过低 -1为过低 1为过高 0为正常
{
    if(status==1)//温度过高
    {
        //打开冷风机打开seconds 秒

        qDebug()<<"打开冷风机";
        emit sendAutoControlAirTemperature();//发送向ModbusManger
        emit sendAutoControlAirTemperatureLED(7);//发送向MainInterface

    }
    else if(status==-1)
    {
        //打开加热器3s
        qDebug()<<"打开加热器";
        emit sendAutoControlAirTemperature();
        emit sendAutoControlAirTemperatureLED(6);

    }
    else if(status==0){

        qDebug()<<"什么也不发生";
        emit sendCloseControlAirTemperatureLEDNormal();
    }
}

void PidController::AutomaticLightIntensityControl(int status)
{
    if(status==1)//光照强度过高
    {
        //打开卷帘门

        qDebug()<<"打开卷帘门";
        emit sendAutoControlLightIntensity();//发送向ModbusManger
        emit sendAutoControlLightIntensityLED(4);//发送向MainInterface

    }
    else if(status==-1)
    {
        //打开补光灯
        qDebug()<<"打开补光灯";
        emit sendAutoControlLightIntensity();
        emit sendAutoControlLightIntensityLED(5);

    }
    else if(status==0){

        qDebug()<<"什么也不发生";
        emit sendCloseControlLightIntensityLEDNormal();
    }

}

void PidController::AutomaticAirHumidityControl(int status)
{
    if(status==1)//湿度过大
    {
        //打开通风设备
        qDebug()<<"打开空气通风设备";
        emit sendAutoControlAirHumidity();//发送向ModbusManger
        emit sendAutoControlAirHumidityLED(2,0);//发送向MainInterface

    }
    else if(status==-1)
    {
        //打开补光灯
        qDebug()<<"打开灌溉--增加空气湿度";
        emit sendAutoControlAirHumidity();//发送向ModbusManger
        emit sendAutoControlAirHumidityLED(1,0);//发送向MainInterface

    }
    else if(status==0){

        qDebug()<<"什么也不发生";
        emit sendCloseControlAirHumidityLEDNormal();
    }

}

void PidController::AutomaticCO2ConcentrationControl(int status)
{
    if(status==1)//CO2浓度过大
    {
        //打开通风设备
        qDebug()<<"打开CO2通风设备";
        emit sendAutoControlCO2Concentration();//发送向ModbusManger
        emit sendAutoControlCO2ConcentrationLED(2,1);//发送向MainInterface

    }
    else if(status==-1)
    {
        //打开CO2添加器
        qDebug()<<"打开CO2添加器";
        emit sendAutoControlCO2Concentration();//发送向ModbusManger
        emit sendAutoControlCO2ConcentrationLED(3,-1);//发送向MainInterface

    }
    else if(status==0){

        qDebug()<<"什么也不发生";
        emit sendCloseControlCO2ConcentrationLEDNormal();
    }
}

void PidController::AutomaticSoilMoistureControl(int status)
{
    if(status==1)//土壤湿度过大
    {
        //打开通风设备
        qDebug()<<"打开土壤通风设备";
        emit sendAutoControlSoilMoisture();//发送向ModbusManger
        emit sendAutoControlSoilMoistureLED(2,2);//发送向MainInterface

    }
    else if(status==-1)
    {

        qDebug()<<"打开灌溉--增加土壤湿度";
        emit sendAutoControlSoilMoisture();//发送向ModbusManger
        emit sendAutoControlSoilMoistureLED(1,1);//发送向MainInterface

    }
    else if(status==0){

        qDebug()<<"什么也不发生";
        emit sendCloseControlSoilMoistureLEDNormal();
    }
}

